package com.gexiaobao.pigeon.ui.adapter

import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.gexiaobao.pigeon.ui.adapter.ImageAdapter.BannerViewHolder
import com.youth.banner.adapter.BannerAdapter

/**
 * @Author : hxw
 * @Date : 2023/3/9 16:10
 * @Describe :
 */
class ImageAdapter(mDatas: List<String?>?) : BannerAdapter<String?, BannerViewHolder>(mDatas) {
    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val imageView = ImageView(parent.context)
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        return BannerViewHolder(imageView)
    }

    override fun onBindView(holder: BannerViewHolder?, data: String?, position: Int, size: Int) {
        holder?.let {
            Glide.with(it.itemView)
                .load(data)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(20)))
                .into(holder.imageView)
        }
    }

    inner class BannerViewHolder(var imageView: ImageView) : RecyclerView.ViewHolder(
        imageView
    )
}